package com.zjw.zy.coreui.view

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.zjw.zy.base.BaseViewGroup
import com.zjw.zy.R
import com.zjw.zy.bean.DailyWeather
import com.zjw.zy.bean.Weather15Day
import com.zjw.zy.ext.logE
import com.zjw.zy.utils.CollectionUtils
import com.zjw.zy.utils.ResourcesUtils
import com.zjw.zy.utils.TimeUtils
import com.zjw.zy.utils.WeatherUtils

/**
 * @author ：zhong.jw
 * @date ：Created in 2022/9/27 18:42
 */
class WeatherSimpleItemView : BaseViewGroup {

    companion object {
        private const val TAG = "MainWeatherView"
    }

    private val mWeatherStateIcon: ImageView by lazy { findViewById(R.id.day_weather_state_icon) }
    private val mWeatherTempText: TextView by lazy { findViewById(R.id.day_weather_state_temp_text) }
    private val mWeatherStateText: TextView by lazy { findViewById(R.id.day_weather_state_text) }
    private val mWeatherCityText: TextView by lazy { findViewById(R.id.day_weather_current_city_text) }
    private val mWeatherDateText: TextView by lazy { findViewById(R.id.day_weather_current_date_text) }
    private val mWeatherRainPercentText: TextView by lazy { findViewById(R.id.day_weather_rain_percent_text) }
    private val mWeatherWindText: TextView by lazy { findViewById(R.id.day_weather_wind_text) }
    private val mWeatherUvText: TextView by lazy { findViewById(R.id.day_weather_uv_text) }
    private val mWeatherRhText: TextView by lazy { findViewById(R.id.day_weather_rh_text) }
    private val mWeatherVisText: TextView by lazy { findViewById(R.id.day_weather_vis_text) }

    private var mListener: WeatherViewListener? = null

    constructor(context: Context?) : super(context)
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    )

    override fun onLayoutId(): Int {
        return R.layout.day_simple_weather_view_layout
    }

    override fun onBindListener() {
        mWeatherStateIcon.setOnClickListener {
            mListener?.onWeather15DayClick()
        }
    }

    override fun onBindValue() {
    }

    override fun onFindView(rootView: View?) {
    }

    fun setWeatherDailyInformation(dailyWeather: DailyWeather) {
        mWeatherDateText.text = ResourcesUtils.getString(
            R.string.weather_date_text,
            TimeUtils.formatDate(dailyWeather.date),
            dailyWeather.week
        )

        mWeatherStateText.text = dailyWeather.textDay
        mWeatherTempText.text =
            ResourcesUtils.getString(
                R.string.weather_temp_text,
                dailyWeather.low,
                dailyWeather.high
            )

        mWeatherRainPercentText.text =
            ResourcesUtils.getString(R.string.weather_rain_percent_text, "${dailyWeather.pop}%")
        mWeatherWindText.text = ResourcesUtils.getString(
            R.string.weather_wind_text,
            dailyWeather.wdDay,
            dailyWeather.wcDay
        )

        mWeatherUvText.text = ResourcesUtils.getString(R.string.weather_uv_text, dailyWeather.uv)

        mWeatherRhText.text = ResourcesUtils.getString(
            R.string.weather_rh_text,
            "${dailyWeather.minRh}%",
            "${dailyWeather.maxRh}%"
        )

        mWeatherVisText.text = ResourcesUtils.getString(R.string.weather_vis_text, dailyWeather.vis.toDouble()/1000)
    }

    fun setWeatherInformation(weather: Weather15Day?) {
        if (weather == null) {
            mWeatherCityText.text = ResourcesUtils.getString(R.string.eror_no_weather)
            return
        }
        val dailyFcsts = weather.dailyFcsts
        //城市名字
        mWeatherCityText.text = weather.location.name

        if (CollectionUtils.isEmpty(dailyFcsts)) {
            "setWeatherInformation isEmpty".logE(TAG)
            mWeatherCityText.text = ResourcesUtils.getString(R.string.eror_no_weather)
            return
        }

        val todayWeather = dailyFcsts[0]

        setWeatherDailyInformation(todayWeather)
        setWeatherIcon(todayWeather)

    }

    private fun setWeatherIcon(todayWeather: DailyWeather) {
        mWeatherStateIcon.setImageDrawable(WeatherUtils.getWeatherIcon(todayWeather))
    }

    fun bindListener(listener: WeatherViewListener) {
        mListener = listener
    }


    interface WeatherViewListener {
        fun onWeather15DayClick()
    }
}